package com.jzg.mvc.biz.cab.picture;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.imageio.ImageIO;

import com.jfinal.core.Path;
import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.Cab;
import com.jzg.common.model.biz.CabPicture;
import com.jzg.common.plugins.shiro.ShiroKit;
import com.jzg.common.plugins.shiro.SimpleUser;
import com.jzg.common.util.FileUtil;
import com.jzg.common.util.UuidUtil;

@Path(value = "/biz/cab/picture", viewPath = "/views/biz/cab/picture")
public class CabPictureController extends BaseController {
	
	public void index() {
		this.setAttr("cabId", this.getPara("cabId"));
		this.render("list.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String name = getPara("name");
		String cabId=getPara("cabId");
		
		String sqlExceptSelect = " from " + CabPicture.TABLE_NAME + " o  where 1=1 ";
		
		if (!StrKit.isBlank(name)) {
			sqlExceptSelect += " and o.name like '%" + name + "%'  ";
		}
		if (!StrKit.isBlank(cabId)) {
			sqlExceptSelect += " and o.cabId = '"+cabId+"' ";
		}
		
		sqlExceptSelect += "  order by   o.createTime desc ";
		Page<CabPicture> page = CabPicture.dao.paginate(pageNumber, pageSize, "select o.* ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	public void allList() {
		List<CabPicture> CabPictures = CabPicture.dao.findAll();
		if(CabPictures!=null && CabPictures.size()>0) {
			renderSuccess("查询成功！", CabPictures);
		}else {
			renderError("无数据！");
		}
	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() {
		CabPicture model = CabPicture.dao.findById(this.getPara("id"));
		if (model != null) {
			if (model.delete()) {
				renderSuccess("删除成功！");
				if(!StrKit.isBlank(model.getUrl())) {
					File file = new File(rootPath+model.getUrl());
					if (file.exists()) {
						file.delete();
					}
				}
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	private String rootPath = PathKit.getWebRootPath();
	public void upload() {
		UploadFile uploadFile = getFile();
		try {
			String cabId=getPara("cabId");
			Cab cab=Cab.dao.findById(cabId);
			if(cab==null) {
				renderError("柜子不存在！");
				return ;
			}
			SimpleUser simpleUser = ShiroKit.getLoginUser();
			
			String path = "/upload/cabPicture" + "/"+cabId+"/" + uploadFile.getFileName().replace(" ", "");
			File file = new File(rootPath+path);
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			if (file.exists()) {
				file.delete();
			}
			java.awt.image.BufferedImage image = ImageIO.read(uploadFile.getFile());
			double width = image.getWidth();
			double height = image.getHeight();
			uploadFile.getFile().renameTo(file);
			CabPicture cabPicture = new CabPicture();
			String id = UuidUtil.getUUID();
			cabPicture.setId(id);
			cabPicture.setCabId(cabId);
			cabPicture.setName(id);
			cabPicture.setWidth(width);
			cabPicture.setHeight(height);
			cabPicture.setUrl(path);
			cabPicture.setFormat(FileUtil.lastName(file));
			cabPicture.setCreateTime(new Date(System.currentTimeMillis()));
			cabPicture.setCreateUserId(simpleUser==null?"":simpleUser.getId());
			cabPicture.setCreateUserName(simpleUser==null?"":simpleUser.getName());
			if(cabPicture.save()) {
				renderSuccess("上传成功！",cabPicture);
			}else {
				renderError("上传失败！");
			}
		} catch (Exception e) {
			renderError(e.getMessage());
		}
	}
	
}
